package com.sys.web.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.servlet.BaseServlet;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.ACate;
import com.sys.goods.pojo.ACateEnum;
import com.sys.goods.pojo.AComment;
import com.sys.goods.pojo.AGoods;
import com.sys.goods.service.IGoodsDbService;
import com.sys.goods.service.impl.GoodsDbServiceImpl;

/**
 * 商品操作
 * 
 * @author 陈岩
 */
@WebServlet(urlPatterns = "/back/GoodsBackServlet")
public class GoodsBackServlet extends BaseServlet
{
	private IGoodsDbService goodsDbService;

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.goodsDbService = new GoodsDbServiceImpl();
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("cateList".equalsIgnoreCase(method))
		{
			/* cateList */
			cateList(request, response);
		} else if ("cateInsert".equalsIgnoreCase(method))
		{
			/* cateInsert */
			cateInsert(request, response);
		} else if ("cateInsertSubmit".equalsIgnoreCase(method))
		{
			/* cateInsertSubmit */
			cateInsertSubmit(request, response);
		} else if ("goodsList".equalsIgnoreCase(method))
		{
			/* goodsList */
			goodsList(request, response);
		} else if ("goodsInsert".equalsIgnoreCase(method))
		{
			/* goodsInsert */
			goodsInsert(request, response);
		} else if ("goodsInsertSubmit".equalsIgnoreCase(method))
		{
			/* goodsInsertSubmit */
			goodsInsertSubmit(request, response);
		}else if ("goodsUpdate".equalsIgnoreCase(method))
		{
			/* goodsUpdate */
			goodsUpdate(request, response);
		} else if ("goodsUpdateSubmit".equalsIgnoreCase(method))
		{
			/* goodsUpdateSubmit */
			goodsUpdateSubmit(request, response);
		}
	}

	/**
	 * 分类列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ACate> apiResponse = this.goodsDbService.findCondListCateService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/goods/cateList";
	}

	/**
	 * 分类添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 页面跳转 */
		return "/back/goods/cateInsert";
	}

	/**
	 * 分类添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cateInsertSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		ACate cate = new ACate();
		String pubTimeStr = request.getParameter("pubTimeStr");
		cate.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));

		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneCateService(cate);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		return apiResponse.toJSON().toJSONString();
	}

	/**
	 * 分类更新
	 * 
	 * @param request
	 * @param response
	 */
	public String cateUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ACate> apiResponse = this.goodsDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/goods/cateUpdate";
		}
		return "/back/goods/cateInfo";
	}

	/**
	 * 分类更新提交
	 * 
	 * @param request
	 * @param response
	 */
	public String cateUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<ACate> apiResponse = this.goodsDbService.findOneCateService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		ACate cate = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String name = request.getParameter("name");
			String url = request.getParameter("url");
			String cateType = request.getParameter("cateType");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			String pubTimeStr = request.getParameter("pubTimeStr");

			/* 赋值 */
			cate.setName(name);
			cate.setUrl(url);
			cate.setCateType(Byte.valueOf(cateType));
			cate.setContent(content);
			cate.setStatus(Byte.valueOf(status));
			cate.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));

			flag = true;
		}

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			apiResponseDb = this.goodsDbService.updateOneCateService(cate);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}

	/**
	 * 商品列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String goodsList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		String cateId = request.getParameter("cateId");
		if (cateId == null)
		{
			cateId = "";
		}
		request.setAttribute("cateId", cateId);
		condMap.put("cateId", cateId);

		String dataType = request.getParameter("dataType");
		if (dataType == null)
		{
			dataType = "";
		}
		request.setAttribute("dataType", dataType);
		condMap.put("dataType", dataType);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findCondListGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
		condMap.put("cateType", ACateEnum.CATETYPE_GOODS.getStatus());
		ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		return "/back/goods/goodsList";
	}

	/**
	 * 商品添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String goodsInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
		condMap.put("cateType", ACateEnum.CATETYPE_GOODS.getStatus());
		ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
		request.setAttribute("cateResponse", cateApiResponse.toJSON());
		return "/back/goods/goodsInsert";
	}

	/**
	 * 商品添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String goodsInsertSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		AGoods goods = new AGoods();
		
		String pubTimeStr = request.getParameter("pubTimeStr");
		goods.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));

		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneGoodsService(goods);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}

	/**
	 * 商品更新
	 * 
	 * @param request
	 * @param response
	 */
	public String goodsUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 查询所有的分类 */
			/* 查询所有的分类;只查询商品的分类 */
			condMap.clear();
			condMap.put("status", ACateEnum.STATUS_ENABLE.getStatus());
			condMap.put("cateType", ACateEnum.CATETYPE_GOODS.getStatus());
			ApiResponse<ACate> cateApiResponse = this.goodsDbService.findCondListCateService(null, condMap);
			request.setAttribute("cateResponse", cateApiResponse.toJSON());
			return "/back/goods/goodsUpdate";
		}
		return "/back/goods/goodsInfo";
	}

	/**
	 * 商品更新提交
	 * 
	 * @param request
	 * @param response
	 */
	public String goodsUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		AGoods goods = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String cateId = request.getParameter("cateId");
			String title = request.getParameter("title");
			String name = request.getParameter("name");
			String brief = request.getParameter("brief");
			String price = request.getParameter("price");
			String marketPrice = request.getParameter("marketPrice");
			String stockNum = request.getParameter("stockNum");
			String dataType = request.getParameter("dataType");
			String content = request.getParameter("content");
			String status = request.getParameter("status");

			/* 赋值 */
			goods.setCateId(Integer.valueOf(cateId));
			goods.setTitle(title);
			goods.setName(name);
			goods.setBrief(brief);
			goods.setPrice(Double.valueOf(price));
			goods.setMarketPrice(Double.valueOf(marketPrice));
			goods.setStockNum(Integer.valueOf(stockNum));
			goods.setDataType(Byte.valueOf(dataType));
			goods.setContent(content);
			goods.setStatus(Byte.valueOf(status));

			String pubTimeStr = request.getParameter("pubTimeStr");
			goods.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));

			flag = true;
		}

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			apiResponseDb = this.goodsDbService.updateOneGoodsService(goods);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}

	/**
	 * 分类列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String commentList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String goodsId = request.getParameter("goodsId");
		if (goodsId == null)
		{
			goodsId = "";
		}
		condMap.put("goodsId", goodsId);
		request.setAttribute("goodsId", goodsId);
		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<AComment> apiResponse = this.goodsDbService.findCondListCommentService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		return "/back/goods/commentList";
	}
}